/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.wiker.codescounter;

import com.wiker.codescounter.bean.CodeBean;
import com.wiker.codescounter.constant.Constants;
import com.wiker.codescounter.counter.JavaCounter;
import com.wiker.codescounter.counter.JspCounter;
import com.wiker.codescounter.counter.PropertiesCounter;
import com.wiker.codescounter.counter.XmlCounter;
import com.wiker.codescounter.util.FileUtils;
import java.io.File;
import java.util.List;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author wiker
 */
public class MainJFrame extends javax.swing.JFrame {

    /**
     * Creates new form MainJFrame
     */
    public MainJFrame() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        projectPath = new javax.swing.JTextField();
        chooseButton = new javax.swing.JButton();
        startButton = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        codeTable = new javax.swing.JTable();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        codeLineNumbersTxt = new javax.swing.JTextField();
        commentLineNumbersTxt = new javax.swing.JTextField();
        blankLineNumbersTxt = new javax.swing.JTextField();
        totalLineNumbersTxt = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        fileLengthTxt = new javax.swing.JTextField();
        fileNumbersTxt = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        javaNumberTxt = new javax.swing.JTextField();
        xmlNumberTxt = new javax.swing.JTextField();
        jspNumberTxt = new javax.swing.JTextField();
        jLabel11 = new javax.swing.JLabel();
        propertiesNumberTxt = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("代码统计工具 - Codes Counter v1.0 by wiker");

        jLabel1.setText("项目路径：");

        projectPath.setEditable(false);
        projectPath.setText("请选择项目路径");
        projectPath.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                projectPathMouseClicked(evt);
            }
        });
        projectPath.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                projectPathFocusGained(evt);
            }
            public void focusLost(java.awt.event.FocusEvent evt) {
                projectPathFocusLost(evt);
            }
        });

        chooseButton.setText("选  择");
        chooseButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                chooseButtonActionPerformed(evt);
            }
        });

        startButton.setText("开始统计");
        startButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                startButtonActionPerformed(evt);
            }
        });

        codeTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "文件", "类型", "扩展名", "总行数", "代码行数", "注释行数", "空行数", "文件大小"
            }
        ));
        jScrollPane1.setViewportView(codeTable);

        jLabel2.setText("代码行数：");

        jLabel3.setText("注释行数：");

        jLabel4.setText("空行数：");

        jLabel5.setText("总行数：");

        jLabel6.setText("文件数：");

        jLabel7.setText("文件总大小(KB):");

        jLabel8.setText("Java文件数：");

        jLabel9.setText("XML文件数：");

        jLabel10.setText("Jsp文件数：");

        jLabel11.setText("properties文件数：");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(startButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(projectPath)
                        .addGap(18, 18, 18)
                        .addComponent(chooseButton))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(jLabel2)
                                        .addGap(18, 18, 18)
                                        .addComponent(codeLineNumbersTxt, javax.swing.GroupLayout.PREFERRED_SIZE, 151, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                        .addGroup(layout.createSequentialGroup()
                                            .addComponent(jLabel4)
                                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                            .addComponent(blankLineNumbersTxt, javax.swing.GroupLayout.PREFERRED_SIZE, 151, javax.swing.GroupLayout.PREFERRED_SIZE))
                                        .addGroup(layout.createSequentialGroup()
                                            .addComponent(jLabel3)
                                            .addGap(18, 18, 18)
                                            .addComponent(commentLineNumbersTxt, javax.swing.GroupLayout.PREFERRED_SIZE, 151, javax.swing.GroupLayout.PREFERRED_SIZE))))
                                .addGap(18, 18, 18)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel7)
                                    .addComponent(jLabel5)
                                    .addComponent(jLabel6))
                                .addGap(18, 18, 18)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(fileNumbersTxt, javax.swing.GroupLayout.PREFERRED_SIZE, 151, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGap(18, 18, 18)
                                        .addComponent(jLabel8))
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(fileLengthTxt, javax.swing.GroupLayout.PREFERRED_SIZE, 151, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGap(18, 18, 18)
                                        .addComponent(jLabel9))
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(totalLineNumbersTxt, javax.swing.GroupLayout.PREFERRED_SIZE, 151, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGap(18, 18, 18)
                                        .addComponent(jLabel10)))
                                .addGap(60, 60, 60))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addComponent(jLabel11)
                                .addGap(18, 18, 18)))
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(javaNumberTxt, javax.swing.GroupLayout.DEFAULT_SIZE, 87, Short.MAX_VALUE)
                            .addComponent(xmlNumberTxt)
                            .addComponent(jspNumberTxt)
                            .addComponent(propertiesNumberTxt))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(projectPath, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(chooseButton))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(startButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 269, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(codeLineNumbersTxt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6)
                    .addComponent(fileNumbersTxt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel8)
                    .addComponent(javaNumberTxt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(commentLineNumbersTxt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel7)
                    .addComponent(fileLengthTxt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel9)
                    .addComponent(xmlNumberTxt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(blankLineNumbersTxt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5)
                    .addComponent(totalLineNumbersTxt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel10)
                    .addComponent(jspNumberTxt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(propertiesNumberTxt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel11))
                .addContainerGap(30, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void projectPathMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_projectPathMouseClicked
        // TODO add your handling code here:
        if(Constants.PROJECT_PATH_TEXT.equals(projectPath.getText())){
            projectPath.setText("");
        }
    }//GEN-LAST:event_projectPathMouseClicked

    private void projectPathFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_projectPathFocusLost
        // TODO add your handling code here:
        projectPath.setText(Constants.PROJECT_PATH_TEXT);
    }//GEN-LAST:event_projectPathFocusLost

    private void projectPathFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_projectPathFocusGained
        // TODO add your handling code here:
        if(Constants.PROJECT_PATH_TEXT.equals(projectPath.getText())){
            projectPath.setText("");
        }
    }//GEN-LAST:event_projectPathFocusGained

    private void chooseButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_chooseButtonActionPerformed
        // TODO add your handling code here:
        JFileChooser chooser = new JFileChooser();
        //FileNameExtensionFilter filter = new FileNameExtensionFilter("excel文件", "xls");
        //chooser.setFileFilter(filter);
        chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        chooser.setDialogTitle("选择一个路径");
        // 得到返回状态
        int returnVal = chooser.showOpenDialog(null);
        chooser.setVisible(true);
        // 如果你点了“取消”按钮。则不操作
        if (returnVal == JFileChooser.CANCEL_OPTION) {
            return;
        }

        // 得到所选文件的路径
        filepath = chooser.getSelectedFile().getAbsolutePath();
        projectPath.setText(filepath);
    }//GEN-LAST:event_chooseButtonActionPerformed

    private void startButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_startButtonActionPerformed
        // TODO add your handling code here:
        totalLineNumbersTxt.setText("0");
        codeLineNumbersTxt.setText("0");
        commentLineNumbersTxt.setText("0");
        blankLineNumbersTxt.setText("0");
        fileNumbersTxt.setText("0");
        fileLengthTxt.setText("0");
        javaNumberTxt.setText("0");
        xmlNumberTxt.setText("0");
        propertiesNumberTxt.setText("0");
        jspNumberTxt.setText("0");
        totalLineNumbers=0;    //总行数
        codeLineNumbers=0;     //代码总行数
        commentLineNumbers=0;  //注释行数
        blankLineNumbers=0;    //空行数
        fileNumbers=0;          //总文件数
        fileSize=0;            //文件大小
        propertiesNumber = 0;  //属性文件数
        xmlNumber = 0;         //xml文件数
        jspNumber = 0;         //jsp文件数
        javaNumber = 0;        //java文件数
        
        
        File file = new File(filepath);
        if(!file.isDirectory()){
            JOptionPane.showMessageDialog(null, Constants.MESSAGE_NO_DIRECTORY, null, JOptionPane.ERROR_MESSAGE);
            return;
        }
        if(!file.exists()){
            JOptionPane.showMessageDialog(null, Constants.MESSAGE_NO_EXIST_DIRECTORY, null, JOptionPane.ERROR_MESSAGE);
            return;
        }
        
        DefaultTableModel model = new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "文件", "类型", "扩展名", "总行数", "代码行数", "注释行数", "空行数", "文件大小"
            }
        );
        
        FileUtils util = new FileUtils();
        List<File> list = util.getFilesExclusive(filepath);
        for(File codeFile : list){
            CodeBean bean = new CodeBean();
            if(codeFile.getName().endsWith(".java")){
                bean = JavaCounter.getByFileForJava(codeFile);
                javaNumber ++;
            }else if(codeFile.getName().endsWith(".properties")){
                bean = PropertiesCounter.getByFileForProperties(codeFile);
                propertiesNumber ++;
            }else if(codeFile.getName().endsWith(".jsp")){
                bean = JspCounter.getByFileForJsp(codeFile);
                jspNumber ++;
            }else if(codeFile.getName().endsWith(".xml")){
                bean = XmlCounter.getByFileForXml(codeFile);
                xmlNumber ++;
            }else{
                continue;
            }
            totalLineNumbers = bean.getTotalLineNumbers() + totalLineNumbers;
            codeLineNumbers = bean.getCodeLineNumbers() + codeLineNumbers;
            commentLineNumbers = bean.getCommentLineNumbers() + commentLineNumbers;
            blankLineNumbers = bean.getBlankLineNumbers() + blankLineNumbers;
            fileNumbers ++;
            fileSize = fileSize + codeFile.length();
            
            String[] codesRow = {codeFile.getPath(),"",bean.getExtName(),String.valueOf(totalLineNumbers),
                String.valueOf(codeLineNumbers),String.valueOf(commentLineNumbers),String.valueOf(blankLineNumbers),String.valueOf(fileSize)};
            model.addRow(codesRow);
        }
        totalLineNumbersTxt.setText(totalLineNumbers+"");
        codeLineNumbersTxt.setText(codeLineNumbers+"");
        commentLineNumbersTxt.setText(commentLineNumbers+"");
        blankLineNumbersTxt.setText(blankLineNumbers+"");
        fileNumbersTxt.setText(fileNumbers+"");
        fileLengthTxt.setText(FileUtils.getFormatSize(fileSize));
        
        codeTable.setModel(model);
        
        javaNumberTxt.setText(String.valueOf(javaNumber));
        xmlNumberTxt.setText(String.valueOf(xmlNumber));
        propertiesNumberTxt.setText(String.valueOf(propertiesNumber));
        jspNumberTxt.setText(String.valueOf(jspNumber));
    }//GEN-LAST:event_startButtonActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(MainJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(MainJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(MainJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(MainJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new MainJFrame().setVisible(true);
            }
        });
    }
    String filepath;
    private long totalLineNumbers=0;    //总行数
    private long codeLineNumbers=0;     //代码总行数
    private long commentLineNumbers=0;  //注释行数
    private long blankLineNumbers=0;    //空行数
    private int fileNumbers=0;          //总文件数
    private long fileSize=0;            //文件大小
    private long propertiesNumber = 0;  //属性文件数
    private long xmlNumber = 0;         //xml文件数
    private long jspNumber = 0;         //jsp文件数
    private long javaNumber = 0;        //java文件数
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField blankLineNumbersTxt;
    private javax.swing.JButton chooseButton;
    private javax.swing.JTextField codeLineNumbersTxt;
    private javax.swing.JTable codeTable;
    private javax.swing.JTextField commentLineNumbersTxt;
    private javax.swing.JTextField fileLengthTxt;
    private javax.swing.JTextField fileNumbersTxt;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField javaNumberTxt;
    private javax.swing.JTextField jspNumberTxt;
    private javax.swing.JTextField projectPath;
    private javax.swing.JTextField propertiesNumberTxt;
    private javax.swing.JButton startButton;
    private javax.swing.JTextField totalLineNumbersTxt;
    private javax.swing.JTextField xmlNumberTxt;
    // End of variables declaration//GEN-END:variables
}
